package com.ruoyi.media.tiktokCover.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.tiktokCover.domain.bo.MediaTiktokCoverBo;
import com.ruoyi.media.tiktokCover.domain.vo.MediaTiktokCoverVo;
import com.ruoyi.media.tiktokCover.domain.MediaTiktokCover;
import com.ruoyi.media.tiktokCover.mapper.MediaTiktokCoverMapper;
import com.ruoyi.media.tiktokCover.service.IMediaTiktokCoverService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 抖音封面列Service业务层处理
 *
 * @author dqw
 * @date 2023-03-23
 */
@RequiredArgsConstructor
@Service
public class MediaTiktokCoverServiceImpl extends ServiceImpl<MediaTiktokCoverMapper, MediaTiktokCover> implements IMediaTiktokCoverService  {

    private final MediaTiktokCoverMapper baseMapper;

    /**
     * 查询抖音封面列
     */
    @Override
    public MediaTiktokCoverVo queryById(Long id){

        return baseMapper.selectVoById(id);
    }

    /**
     * 查询抖音封面列列表
     */
    @Override
    public TableDataInfo<MediaTiktokCoverVo> queryPageList(MediaTiktokCoverBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaTiktokCover> lqw = buildQueryWrapper(bo);
        Page<MediaTiktokCoverVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询抖音封面列列表
     */
    @Override
    public List<MediaTiktokCoverVo> queryList(MediaTiktokCoverBo bo) {
        LambdaQueryWrapper<MediaTiktokCover> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaTiktokCover> buildQueryWrapper(MediaTiktokCoverBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaTiktokCover> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaTiktokCover::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaTiktokCover::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserIndex()), MediaTiktokCover::getUserIndex, bo.getUserIndex());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), MediaTiktokCover::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverUrl()), MediaTiktokCover::getCoverUrl, bo.getCoverUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getUniqueId()), MediaTiktokCover::getUniqueId, bo.getUniqueId());
        lqw.eq(StringUtils.isNotBlank(bo.getShowCity()), MediaTiktokCover::getShowCity, bo.getShowCity());
        lqw.eq(StringUtils.isNotBlank(bo.getIpCity()), MediaTiktokCover::getIpCity, bo.getIpCity());
        lqw.eq(StringUtils.isNotBlank(bo.getSignature()), MediaTiktokCover::getSignature, bo.getSignature());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), MediaTiktokCover::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getFansNum()), MediaTiktokCover::getFansNum, bo.getFansNum());
        lqw.eq(StringUtils.isNotBlank(bo.getAge()), MediaTiktokCover::getAge, bo.getAge());
        lqw.eq(bo.getGender() != null, MediaTiktokCover::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getTotalFavorited()), MediaTiktokCover::getTotalFavorited, bo.getTotalFavorited());
        lqw.eq(StringUtils.isNotBlank(bo.getFriend()), MediaTiktokCover::getFriend, bo.getFriend());
        lqw.eq(StringUtils.isNotBlank(bo.getVisitor()), MediaTiktokCover::getVisitor, bo.getVisitor());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverLinks()), MediaTiktokCover::getCoverLinks, bo.getCoverLinks());
        lqw.eq(bo.getShowType() != null, MediaTiktokCover::getShowType, bo.getShowType());
        lqw.eq(bo.getIsUse() != null, MediaTiktokCover::getIsUse, bo.getIsUse());
        return lqw;
    }

    /**
     * 新增抖音封面列
     */
    @Override
    public Boolean insertByBo(MediaTiktokCoverBo bo) {
        MediaTiktokCover add = BeanUtil.toBean(bo, MediaTiktokCover.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改抖音封面列
     */
    @Override
    public Boolean updateByBo(MediaTiktokCoverBo bo) {
        MediaTiktokCover update = BeanUtil.toBean(bo, MediaTiktokCover.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaTiktokCover entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除抖音封面列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
